// import Vue from 'vue'
// import App from './App.vue'
//
// Vue.config.productionTip = false
//
// new Vue({
//   render: h => h(App),
// }).$mount('#app')

import Vue from 'vue/dist/vue'
import App from './App.vue'

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

import '../public/css/base.css';

import VueRouter from 'vue-router';
import VueResource from 'vue-resource';

//客户管理
import  CustomerInfo from 'components/HLCustomer/CustomerInfo'                //客户信息
import  Contacts from 'components/HLCustomer/Contacts'                        //我的联系人

//合同管理
import ContractInfo from 'components/HLContract/ContractInfo'                 //合同基本信息
import DeviceMatching from 'components/HLContract/DeviceMatching'             //设备匹配
import DeliverGoodsHistory from 'components/HLContract/DeliverGoodsHistory'   //发货信息

//产品管理
import HLProductINfo from 'components/HLProduct/HLProductINfo'                //产品信息
import HLQualityTesting from 'components/HLProduct/HLQualityTesting'          //质检

import Login from 'components/login/login.vue'

// import common from '../public/js/common.js'

// import  routes from'./router/router.js'

import VueParticles from 'vue-particles';
import axios from 'axios' ;
import Vuex from 'vuex' //引入状态管理

Vue.config.productionTip = false;

Vue.use(VueRouter);
Vue.use(VueResource);
Vue.use(ElementUI);



// import iView from 'iview';
// import 'iview/dist/styles/iview.css';
// import locale from 'iview/dist/locale/en-US';


Vue.use(VueParticles) ;
// Vue.use(iView, { locale });
Vue.config.productionTip = false ;
Vue.prototype.$ajax = axios ;
Vue.use(Vuex);


const ADD_COUNT = 'ADD_COUNT'; // 用常量代替事件类型，使得代码更清晰
const REMOVE_COUNT = 'REMOVE_COUNT';


//注册状态管理全局参数
var store = new Vuex.Store({
  state:{
    token:'',
    userID:'',
  },
  mutations: {
    //写法与getters相类似
    //组件想要对于vuex 中的数据进行的处理
    //组件中采用this.$store.commit('方法名') 的方式调用，实现充分解耦
    //内部操作必须在此刻完成(同步)
    [ADD_COUNT] (state, token) { // 第一个参数为 state 用于变更状态 登录
      sessionStorage.setItem("token", token);
      state.token = token;
    },
    [REMOVE_COUNT] (state, token) { // 退出登录

      sessionStorage.removeItem("token", token);

      state.token = token;
    },
  }
});


// router.afterEach(route => {
//   // iView.LoadingBar.finish();
// });





const routes = [
  {
    path: '/HLCustomer/CustomerInfo',
    name: 'CustomerInfo',
    component: CustomerInfo
  },
  {
    path: '/HLCustomer/Contacts',
    name: 'Contacts',
    component: Contacts,
    meta: {
      requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
    },

  },


  {
    path: '/HLContract/ContractInfo',
    name: 'ContractInfo',
    component: ContractInfo
  },
  {
    path: '/HLContract/DeviceMatching',
    name: 'DeviceMatching',
    component: DeviceMatching
  },
  {
    path: '/HLContract/DeliverGoodsHistory',
    name: 'DeliverGoodsHistory',
    component: DeliverGoodsHistory
  },


  {
    path: '/HLProduct/HLProductINfo',
    name: 'HLProductINfo',
    component: HLProductINfo
  },
  {
    path: '/HLProduct/HLQualityTesting',
    name: 'HLQualityTesting',
    component: HLQualityTesting
  }//登录
  ,{
    path: '/login',
    name: 'Login',
    component: Login
  },
];

const router = new VueRouter({
  routes, // (缩写) 相当于 routes: routes
  linkActiveClass :"active"
})




router.beforeEach((to, from, next) => {
  // iView.LoadingBar.start();//loadong 效果

  store.state.token = sessionStorage.getItem('token');//获取本地存储的token
    // console.log(998)
  if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
    if (store.state.token !== "" && store.state.token !== null) {  // 通过vuex state获取当前的token是否存
      next();
    }
    else {
      next({
        path: '/login',
        query: {redirect: to.fullPath}  // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    }
  }
  else {
    next();
  }
})

// router.beforeEach((to,from,next) => {
//   if(to.matched.some( m => m.meta.auth)) {
//     // console.log("先判断是否登录");
//     if(to.name=='Login'){
//       next();
//     }else{
//       if(localStorage.getItem('token')){
//
//         //访问服务器缓存数据，判断当前token是否失效
//         Vue.http.get("http:xxxx/Login/UserIsLogin?token="+localStorage.getItem('token')+"&url="+to.name,{withCredentials: true}).then(response => response.json()).then(num => {
//           // console.log('是否登录',num);
//           if(num.code==1001){
//             next();
//           }else{
//             alert('您的token已超时，请重新登录');
//             next('/Login');
//           }
//         })
//       }else{
//         next('/Login');
//       }
//
//     }
//   } else {
//     console.log("请先登录");
//     next()
//   }
// })



// let hasLogin = false // 是否登录过
// // 登录拦截
// router.beforeEach((to, from, next) => {
//   if (document.cookie.includes('userName')) {
//     hasLogin = true
//     if (permission.indexOf(to.path) > -1) {
//       next()
//     } else {
//       try {
//         // 简单的权限判断
//         let routesKey = Cookie.get('routesKey').split(',')
//         let key = to.path.split('/')[1]
//         if (routesKey.indexOf(key) > -1) {
//           next()
//         } else {
//           next({path: '/'})
//         }
//       } catch (e) {
//         next({path: '/login'})
//       }
//     }
//   } else {
//     if (to.path === '/login') { // 如果是登录页面路径，就直接next()
//       next()
//     } else {
//       if (hasLogin) {
//         Cookie.clear('userName')
//         ELEMENT.Message({
//           message: '登录失效,请重新登录!',
//           center: true,
//           type: 'warning'
//         })
//       }
//       next({path: '/login', query: {redirect: to.fullPath}})
//     }
//   }
// })








new Vue({
  router,
  render: h => h(App),
}).$mount('#app');

